package cn.javaxc.model.base;
import java.util.*;
import java.sql.*;
import javax.servlet.http.*;

import cn.javaxc.mvcwork.util.common.CommonUtil;
import cn.javaxc.mvcwork.util.helper.URLHelper;
import cn.javaxc.mvcwork.util.common.PasswordHash;

import cn.javaxc.mvcwork.controller.*;
import cn.javaxc.mvcwork.util.common.*;
import cn.javaxc.mvcwork.util.helper.*;




public  class SQL{

    public static int getYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int getMonth() {
        return Calendar.getInstance().get(Calendar.MONTH)+1;
    }
    public static int getDay() {
        return Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
    }
    public static String getDate() {
        return getYear()+"-"+getMonth()+"-"+getDay();
    }
	public static String where(Object value,String where){
		if (value instanceof Collection){
			if (((Collection)value).size()>0){
				return where;			
			}
		}else if (value instanceof Object[]){
			if (((Object[])value).length>0){
				return where;			
			}
		}else if (!StringUtils.isNullData(value)){
			return where;
		}
		return "";
	}
	/**
	 * 是否匹配字符规则,可以进行身份证等规则的检查
	 * @param key 参数名
	 * @param reg 规则表达式
	 * @return  是返回1,不是返回0
	 */

	public static int matches(String value,String reg){
		if ("".equals(value.trim())){
			return 1;
		}else if (value.trim().matches(reg)){
			return 1;
		}else{
			return 0;
		}
	}
	/**
	 *是否为邮件
	 * @return  是返回1,不是返回0
	 */

	public static int isEmail(String key,String value){
		return matches(value,"^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");

	}
	/**
	 *是否为数字
	 * @return  是返回1,不是返回0
	 */

	public static int isNumber(String key,String value){
		return matches(value,"[0-9]+");
	}
	/**
	 * <pre>
	 * &lt;c:forEach var="listKey" items='${value.split(listSymbol)}'&gt;
	 *   update table set :listKey ...
	 * &lt;/c:forEach&gt;
	 * </pre>
	 * @param key 参数名
	 * @param value 分隔后的参数名
	 * @param symbol 分隔符,默认为逗号(在textarea中则为\n)
	 */

	public static SQLList<SQLMap> foreach(String value,String listKey,String listSymbol){
		if (value.isEmpty()){
			return new SQLList();
		}
		SQLList l_list=new SQLList();
		if (listKey!=null&&!"".equals(listKey)){
			if (value!=null){
				String[] v_arr=value.split(listSymbol);
				for (int i=0;i<v_arr.length;i++){
					Map m=new HashMap();
					m.put(listKey,v_arr[i].trim());
					l_list.appendItem((i+1)+"",v_arr[i].trim(),m);
				}
			}
		}
		return l_list;
	}

	/**
	 * <pre>
	 * &lt;c:forEach var="key" items='${eachMap}'&gt;
	 *   update table set :key :{eachMap['key']} ... 
	 * &lt;/c:forEach&gt;
	 * </pre>
	 */

	public static SQLList<SQLMap> foreach(Map eachMap,String listKey,String listSymbol){
		
		SQLList l_list=new SQLList();
		if (eachMap!=null){
			if (listKey!=null&&!"".equals(listKey)){
				if (listSymbol!=null&&!"".equals(listSymbol)){
					for (Object eachKey:eachMap.keySet() ){
						Map m=new HashMap();
						m.put(listKey,eachKey);
						m.put(listSymbol,eachMap.get(eachKey));
						l_list.appendItem("","",m);
					}
				}
			}
		}
			
		return l_list;
	}

	public static Integer setIntTime(Integer _def,String hour,String minute,String second){
		int v_hour=CommonUtil.parseInt(hour,-1);
		int v_minute=CommonUtil.parseInt(minute,0);
		int v_second=CommonUtil.parseInt(second,0);
		if (v_hour<0){
			return _def;
		}else{
			if(v_minute<0)v_minute=0;
			if(v_second<0)v_second=0;
			return v_hour*3600+v_minute*60+v_second;
		}
	}
	/**
	 * 处理日期数据<br/>
	 * 示例 :{setTime(null,startDay,startHour,startMinute)}<br/>
	 * 返回 'startDay startHour:startMinute'<br/>
	 * @param _def 默认值,若date为空,则使用默认值
	 * @param date 日期
	 * @param hour 小时
	 * @param minute 分钟
	 */
	public static String setDateTime(String _def,String v_date,String v_hour,String v_minute,String v_second){

		if (v_date==null)return null;
		if(CommonUtil.parseInt(v_hour,-1)<0)v_hour="0";
		if(CommonUtil.parseInt(v_minute,-1)<0)v_minute="0";
		if(CommonUtil.parseInt(v_second,-1)<0)v_second="0";
		if("".equals(v_date.toString().trim())) {
			return _def;
		}else{
			return (v_date+" "+v_hour+":"+v_minute+":"+v_second).trim();
		}
	}
	public static String setTime(String _def,String v_hour,String v_minute,String v_second){


			
		if(CommonUtil.parseInt(v_hour,-1)<0){
			return _def;
		}else{
			if(CommonUtil.parseInt(v_minute,-1)<0)v_minute="0";
			if(CommonUtil.parseInt(v_second,-1)<0)v_second="0";
			
			return v_hour+":"+v_minute+":"+v_second;
		}
	}

	public static String[] split(String s,String sp){
		if (StringUtils.isNullData(s)){
			return new String[0];
		}
		return s.trim().split(sp);
	}

	/**
	 * 将参数转为Int,参数值可来源于数组<br/>
	 * 若数组中没有相对的值,使用默认值<br/>
	 * 示例 :{setIntFromArray(CMID,5个,`5个=1,10个=2,6个=3`)}<br/>
	 *		若CMID未定义,则返回"5个",若CMID为2,则返回"10个"
	 * @param key 参数名
	 * @param _def 默认值
	 * @param values 数组值
	 */
	public static String setIntArray(Object value,String _def,String values){
		String _value=_def;
		for (String s:values.split(",") ){
			String[] option=s.split("=");
			if (option.length==2){
				if (option[0].equals(value)){
					_value=option[1];
					break;
				}
			}else if (option.length==1){
				if (option[0].equals(value)){
					_value=option[0];
					break;
				}
			}
			
		}
		return _value==null?"":_value;
		
	}
	/**
	 * 将参数转为String 同setIntFromArray
	 *@see cn.javaxc.mvcwork.controller.web.SQL#setIntFromArray(String,String,String)
	 */

	public static String setStringArray(Object value,String _def,String values){
		String ret=setIntArray(value,_def,values);
		if (!StringUtils.isNullData(ret)){
			return quote(ret);
		}
		return ret;
	}


	public static int setInt(Object value){
		return CommonUtil.parseInt(value,-1);
	}
	public static int setInt(Object value,Integer _def){
		return CommonUtil.parseInt(value,_def);
	}
	public static String quote(Object value){
		if (value==null){
			return null;
		}
		return "'"+value+"'";
	}
	public static boolean isNull(Object value){
		return StringUtils.isNullData(value);
	}
	public static boolean isNotNull(Object value){
		return !StringUtils.isNullData(value);
	}
	public static String setPageId(String value){
		return URLHelper.getPageId(value);
	}
	public static String setRandom(Integer length){
		StringBuffer randomCode = new StringBuffer();
		Random rand = new Random();
		// 随机生成验证码
		for (int i = 0; i < length; i++) {
			String randStr = String.valueOf(MVCConstant.code[rand.nextInt(MVCConstant.code.length)]);
			randomCode.append(randStr); // 将随机产生的字符合成一个字符串
		}
		return randomCode.toString();
	}
	public static String md5(String value,String _def){
		value=isNull(value)?_def:value; 
		if ("false".equals(MVCConfig.getConfig().getCheckMD5Password())){
			return value;
		}
		return PasswordHash.encrypt(value);
	}

	public static Object getParameter(HttpServletRequest request,Map map,String para,Object _def){
		return RequestHelper.getRequestValue(request,map,para,_def);
	}

	public static void setParameter(Map m,String k,Object v){
		if (v!=null){
			putParameter(m,k,v);
		}
	}
	public static Object putParameter(Map m,String k,Object v){
//		if ("".equals(v)){
//			m.put(k,null);
//		}
		m.put(k,v);
		return m.get(k);
	}
	public static void likeParameter(Map m,String k,Object v){
		m.put(k,"%"+v+"%");
	}
	public static String uuid(){
		return StringUtils.createUUID();
	}
	public static List asList(Object... object_arr){
		return CommonUtil.asList(object_arr);
	}
	public static String getListTable(String table){
		return table;
	}
}
